Use case: Encoder interface in interrupt mode ¶
|
HAL1 |
HAL2 |
|---|---|
|
Initialization and configuration
Start encoder interface in interrupt mode
TIMx IRQ handler
Callback function
|
Initialization and configuration
Start encoder interface in interrupt mode
TIMx IRQ handler
Callback function
|
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
/* Declaration of tim handle */
TIM_HandleTypeDef htim3;
/* Ensure that all required peripherals
(TIM(timer, channel), GPIO, NVIC, etc.) are configured properly
before starting encoder mode.*/
.............
/* Start the encoder mode in interrupt */
HAL_TIM_Encoder_Start_IT(&htim3, TIM_CHANNEL_ALL);
/* User must call the Interrupt Handler
functions in TIM3 global interrupt */
void TIM3_IRQHandler(void)
{
/* Code to execute at each interrupt */
HAL_TIM_IRQHandler(&htim3);
}
/* Call the callback function */
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
/* Get the Input Capture value */
uwIC2Value = HAL_TIM_ReadCapturedValue(&htim3, TIM_CHANNEL_2);
}
|
/* Declaration of the tim handle */
hal_tim_handle_t htim3;
/* Ensure that all required peripherals
(TIM(timer, channel), GPIO, NVIC, etc.) are configured properly
before starting encoder mode.*/
.............
/* Start the encoder mode in interrupt */
HAL_TIM_IC_StartChannel_IT(&htim3, HAL_TIM_CHANNEL_1);
HAL_TIM_IC_StartChannel_IT(&htim3, HAL_TIM_CHANNEL_2);
HAL_TIM_Start(&htim3);
/* User must call the Interrupt Handler
functions in TIM2 global interrupt */
void TIM3_IRQHandler(void)
{
/* Code to execute at each interrupt */
HAL_TIM_IRQHandler(&htim3);
}
/* Call the callback function */
void HAL_TIM_InputCaptureCallback(hal_tim_handle_t *htim,
hal_tim_channel_t channel);
{
/* Get the Input Capture value */
uwIC2Value = HAL_TIM_IC_ReadChannelCapturedValue(&htim3, HAL_TIM_CHANNEL_2)
}
|
Key changes ¶
Encoder initialization is unified in HAL2 using
HAL_TIM_Init()+HAL_TIM_SetConfig()and channel configuration uses input-capture configuration APIs.Encoder start in interrupt mode is performed with per-channel start APIs and requires
HAL_TIM_Start().
![@startuml
title "Encoder Mode in HAL 1"
participant "User Application" as App
participant "HAL TIM" as Driver
participant "MSP" as MSP
group Initialization and Configuration
App -> Driver: <b><font color=purple>HAL_TIM_Encoder_Init</b>(&(TIM_Encoder_InitTypeDef){\nSet Instance (TIMx)\nSet Init structure\nEncoderMode = TIM_ENCODERMODE_TI12,\nIC1Polarity = TIM_ICPOLARITY_RISING,\nIC1Selection = TIM_ICSELECTION_DIRECTTI,\nIC1Prescaler = TIM_ICPSC_DIV1,\nIC1Filter = 0,\nIC2Polarity = TIM_ICPOLARITY_RISING,\nIC2Selection = TIM_ICSELECTION_DIRECTTI,\nIC2Prescaler = TIM_ICPSC_DIV1,\nIC2Filter = 0})
Driver -> MSP: HAL_TIM_ENCODER_MspInit()
MSP -> Driver: Enable TIMx clock\nCH1, CH2 GPIO AF config
end
group Start TIMx in IT Mode
App -> Driver: <b><font color=green>HAL_TIM_Encoder_Start_IT</b>(TIM_CHANNEL_ALL)
Driver->Driver : Enable capture compare interrupt\nEnable TIMx counter
Driver --> App : HAL_OK
IT <[#red]-] : <font color=red><b>TIMx IRQ
IT -> Driver : HAL_TIM_IRQHandler()
Driver -> Driver : Clear flag
Driver -> App: HAL_TIM_IC_CaptureCallback()
App --> Driver
Driver --> IT
end
@enduml](../../../../../_images/plantuml-70c4e259869952a4fe615bf469337dcf2098dc47.png)
![@startuml
skinparam maxMessageSize 440
' To add a number by line
'autonumber
title "Encoder Mode in HAL 2"
' Fix order of each column
participant "User Application" as App
participant "HAL TIM" as Driver
group Initialization
App -> Driver : <b><font color=purple>HAL_TIM_Init()</b>
Driver -> MSP: TIM_MspInit()
MSP -> Driver: Enable TIMx clock\nEnable TIMx interrupt
Driver --> App : HAL_OK
end
group Configuration
App->Driver : <b><font color=purple>HAL_TIM_SetConfig</b>((hal_tim_config_t) {\n.prescaler = α\n.counter_mode = HAL_TIM_COUNTER_UP,\n.period = β,\n.clock_source = HAL_TIM_CLK_ENCODER_X4_TI12}
Driver -> Driver : Configure TIMx time base
Driver --> App : HAL_OK
App -> Driver : <b><font color=purple>HAL_TIM_IC_SetConfigChannel</b>(channel = HAL_TIM_CHANNEL_1,\nhal_tim_ic_channel_config_t {\nsource = HAL_TIMx_TI1_GPIO,\npolarity = HAL_TIM_IC_POLARITY_RISING,\nfilter = HAL_TIM_FDIV1})
Driver --> App : HAL_OK
App -> Driver : <b><font color=purple>HAL_TIM_IC_SetConfigCaptureUnit</b>(capture_unit = HAL_TIM_CAPTURE_UNIT_1,\nhal_tim_ic_capture_config_t {\nactive_input = HAL_TIM_IC_DIRECT,\nprescaler = HAL_TIM_IC_DIV1})
Driver --> App : HAL_OK
App -> Driver : <b><font color=purple>HAL_TIM_IC_SetConfigChannel</b>(channel = HAL_TIM_CHANNEL_2,\nhal_tim_ic_channel_config_t {\nsource = HAL_TIMx_TI2_GPIO,\npolarity = HAL_TIM_IC_POLARITY_RISING,\nfilter = HAL_TIM_FDIV1})
Driver --> App : HAL_OK
App -> Driver : <b><font color=purple>HAL_TIM_IC_SetConfigCaptureUnit</b>(capture_unit = HAL_TIM_CAPTURE_UNIT_2,\nhal_tim_ic_capture_config_t {\nactive_input = HAL_TIM_IC_DIRECT,\nprescaler = HAL_TIM_IC_DIV1})
Driver --> App : HAL_OK
end
group Start encoder interface in IT mode
App -> Driver : <b><font color=green>HAL_TIM_IC_StartChannel_IT</b>(HAL_TIM_CHANNEL_1)
Driver --> App : HAL_OK
App -> Driver : <b><font color=green>HAL_TIM_IC_StartChannel_IT</b>(HAL_TIM_CHANNEL_2)
Driver --> App : HAL_OK
Driver->Driver : Enable the channel\nEnable capture compare interrupt.
Driver --> App : HAL_OK
IT <[#red]-] : <font color=red><b>TIMx IRQ
IT -> Driver : HAL_TIM_IRQHandler()
Driver -> Driver : Clear flag
App -> Driver : <b><font color=green>HAL_TIM_Start</b>()
Driver->Driver : Enable TIMx counter
Driver --> App : HAL_OK
Driver -> App: HAL_TIM_InputCaptureCallback()
end
@enduml](../../../../../_images/plantuml-61b0e989066935bdc4b1b4115c214d6e9c37b90c.png)